/**
 * Created by Anard on 2017/4/20.
 */

let mem = require('../util/mem.js');
let log = require('../util/log.js');
let err = require('../data/err.js');
let rpc = require('./rpc.js');
//let action = require('./action.js');
let gameData = require('../util/gameData.js');

let push = module.exports;

push.sendError = function (code, id) {
    let o = {};
    o.action = 'error';
    o.uuid = id;
    o.obj = {'code': code};
    push.error(o);
};

push.path = function (obj) {
    gameData.getMessage(obj);
    if (!obj.uuid) {
        return false;
    }
    if (typeof obj.uuid == 'object') {
        obj.uuid.forEach(function (uuid) {
            let objc = {'action': obj.action, 'uuid': uuid, 'obj': obj.obj};
            push.call(objc);
        });
    } else {
        push.call(obj);
    }
};

push.call = function (obj) {
    if (push && push[obj.action]) {
        push[obj.action](obj);
    } else {
        push.public(obj);
    }
};

push.public = function (obj) {
    if (!mem.clients[obj.uuid]) {
        return false;
    }
    let send = {};
    send.action = obj.action;
    send.data = obj.obj;
    push.send(obj.uuid,send);
    return true;
};

push.error = function (obj) {
    if (!mem.clients[obj.uuid]) {
        return false;
    }
    obj.obj.msg = err[obj.obj.code];
    let send = {};
    send.action = obj.action;
    send.data = obj.obj;
    push.send(obj.uuid,send);
    return true;
};

push.heartbeat = function (uuid, time) {
    if (!mem.clients[uuid]) {
        return false;
    }
    let send = {};
    send.action = 'globalHeartbeat';
    send.data = {'time': time};
    push.send(uuid,send);
    return true;
};

push.enterKick = function (obj) {
    //push.public(obj);
    for (let i in mem.clients) {
        if (i == obj.uuid) {
            continue;
        }
        if (mem.clients[i].user && mem.clients[i].user.id == obj.obj.id) {
            let send = {};
            send.action = 'error';
            send.data = {'code': 10000};
            push.send(i,send);
            try {
                //action.close(mem.clients[i]);
                mem.clients[i].close();
            } catch (e) {
                log.e(e);
            }
        }
    }
};

push.alertMsg = function (obj) {
    for (let i in mem.clients) {
        let send = {};
        send.action = 'globalAlert';
        send.data = {'text': obj.obj.text, 'title': null};
        push.send(i,send);
    }
};

push.send = function(uuid,send){
    try {
        if (send.action != 'globalHeartbeat') {
            log.push('push', JSON.stringify(send));
        }
        mem.clients[uuid].send(JSON.stringify(send));
    } catch (e) {
        //log.e(e);
    }
};

push.enterLogin = function (obj) {
    if (!push.public(obj)) {
        return false;
    }
    if (mem.clients[obj.uuid]) {
        mem.clients[obj.uuid].user = obj.obj;
        delete mem.clients[obj.uuid].user.serverTime;
    }
    console.log('push.enterLogin');
    console.log(mem.clients[obj.uuid].user);
    rpc.send.dc('dcLink', obj.uuid, mem.clients[obj.uuid].user.id, {
        'weixinId': mem.clients[obj.uuid].user.weixinId,
        'bluedId': mem.clients[obj.uuid].user.bluedId,
    });
};
push.enterChip = function (obj) {
    if (!push.public(obj)) {
        return false;
    }
    if (mem.clients[obj.uuid]) {
        mem.clients[obj.uuid].chip = obj.obj;
    }
};

push.enterLogout = function (obj) {
    if (!push.public(obj)) {
        return false;
    }
    //action.close(mem.clients[obj.uuid]);
    mem.clients[obj.uuid].close();
};

push.globalUserChange = function (obj) {
    if (!push.public(obj)) {
        return false;
    }
    if (!mem.clients[obj.uuid].user) {
        return false;
    }
    for (let i in obj.obj) {
        mem.clients[obj.uuid].user[i] = obj.obj[i].now;
    }
};

push.globalChipChange = function (obj) {
    if (!push.public(obj)) {
        return false;
    }
    if (!mem.clients[obj.uuid].chip) {
        return false;
    }
    for (let i in obj.obj) {
        mem.clients[obj.uuid].chip[i] = obj.obj[i].now;
    }
};

push.matchGame = push.matchCustom = push.matchFollow = function (obj) {
    if (!push.public(obj)) {
        return false;
    }
    if (!mem.clients[obj.uuid].user) {
        return false;
    }
    mem.clients[obj.uuid].user.gameId = obj.obj.gameId;
};

push.gameIdClear = function (obj) {
    if (!push.public(obj)) {
        return false;
    }
    if (!mem.clients[obj.uuid].user) {
        return false;
    }
    mem.clients[obj.uuid].user.gameId = null;
};